#!/usr/bin/python3
import os
import time
import pandas as pd

session_file_path = r'../LongTimeSession.csv'


def main():
    # localeId,deviceId,sessionId,sessionEndedAt,sessionEndedAt
    # session_cvs = pd.read_csv(session_file_path, sep=';')
    session_cvs = pd.read_csv(session_file_path, sep=',')

    deviceIds = session_cvs['deviceId']
    localeIds = session_cvs['localeId']
    sessionIds = session_cvs['sessionId']
    sessionStartedAts = session_cvs['sessionEndedAt']
    sessionEndedAts = session_cvs['sessionEndedAt']

    device_locale_id = {}
    device_session_infos = {}
    for deviceId, localeId, sessionId, sessionStartedAt, sessionEndedAt in zip(deviceIds, localeIds, sessionIds, sessionStartedAts, sessionEndedAts):
        device_locale_id[deviceId] = localeId
        sessionStartedAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sessionStartedAt) // 1000 + 3600 * 8))
        sessionEndedAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(sessionEndedAt) // 1000 + 3600 * 8))
        session_infos = device_session_infos.get(deviceId, None)
        if session_infos is None:
            device_session_infos[deviceId] = [[sessionId, sessionStartedAt, sessionEndedAt]]
        else:
            session_infos.append([sessionId, sessionStartedAt, sessionEndedAt])

    for device_id, locale_id in device_locale_id.items():
        print('Device: {}, Locale: {}'.format(device_id, locale_id))
        session_infos = device_session_infos[device_id]
        for session_id, sessionStartedAt, sessionEndedAt in session_infos:
            print('  Session: {}, {} - {}'.format(session_id, sessionStartedAt, sessionEndedAt))


if __name__ == '__main__':
    main()
